package org.example.service.impl;

import org.example.mapper.VolunteerAnalyzeMapper;
import org.example.pojo.ListMap;
import org.example.pojo.Volunteer;
import org.example.pojo.activeMap;
import org.example.pojo.volunteerDataCount;
import org.example.service.VolunteerDataAnalyzeService;
import com.google.gson.Gson;
import jakarta.annotation.Resource;
import org.example.utils.CharData;
import org.example.utils.R;
import org.example.utils.SeriesData;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Description:
 * @Author:曹震
 * @Date: 2024/12/4 17:04
 */

@Service
public class VolunteerDataAnalyze implements VolunteerDataAnalyzeService {
    @Resource
    private VolunteerAnalyzeMapper mapper;
    @Override
    public List<Volunteer> sortVolunteers() {
        List<Volunteer> volunteers = mapper.sortListVolunteer();
       return volunteers;
    }
    @Override
    public Volunteer listVolunteerTask(String volunteerId) {
        Volunteer volunteer = mapper.listVolunteerTask(volunteerId);
        return volunteer;
    }

    @Override
    public String listMap(String volunteerId) {
        HashMap<String, List<ListMap>> stringListMapHashMap = new HashMap<>();
        //第一种：志愿者评价
        List<ListMap>list=mapper.accountVolunteersEvaluate(volunteerId);
        //任务种类分布
        List<ListMap> listMaps = mapper.accountTaskType(volunteerId);
        stringListMapHashMap.put("志愿者评价等级统计", list);
        stringListMapHashMap.put("任务种类分布", listMaps);
        Gson gson = new Gson();
        String json = gson.toJson(stringListMapHashMap);
        return json;
    }

    @Override
    public List<Map<String,Integer>> showActiveMap() {
        List<Map<String, Integer>> maps = mapper.activeMap();
        System.out.println(maps);
        return maps;
    }

    @Override
    public Map<String, Integer> showRating() {
        List<Integer> integers = mapper.listVolunteerRating();
        Map<String,Integer>map = new HashMap<>();
        int [] rating = new int[5];
        for(int i=0;i<integers.size();i++){
            if(integers.get(i)<=1){
                rating[0]++;
            }else if(integers.get(i)<=2){
                rating[1]++;
            }else if(integers.get(i)<=3){
                rating[2]++;
            }else if(integers.get(i)<=4){
                rating[3]++;
            }else{
                rating[4]++;
            }
        }

            map.put("五星",rating[4]);
            map.put("四星",rating[3]);
            map.put("三星",rating[2]);
            map.put("二星",rating[1]);
            map.put("一星",rating[0]);
            return map;

    }

    @Override
    public String getVolunteerCountByMonth() {
       List<volunteerDataCount> accountsByTime = mapper.findMonthlyCountByTime();
       List<Integer>allAccount = mapper.findMonthlyAllCount();
       //准备x的数据(日期)
        List<String> months = new ArrayList<>();
        List<Integer> accountByMonth = new ArrayList<>();
        for(volunteerDataCount temp :accountsByTime){
            months.add(temp.getData());
            accountByMonth.add(temp.getAccounts());
        }
        SeriesData seriesData = new SeriesData("当前月份志愿者人数", accountByMonth, "bar", true, "blue");
        SeriesData seriesData1 = new SeriesData(null, allAccount, null, null, null);
        CharData charData = new CharData();
        charData.setXAxis(months);
        charData.setSeriesData(Arrays.asList(seriesData,seriesData1));
        Gson gson = new Gson();
        return gson.toJson(charData);
    }
    @Override
    public String getVolunteerCountByWeek(String data) {
        List<volunteerDataCount> accountByTime = mapper.findWeekCountByTime(data);
        System.out.println("这是数据库里的数据:"+accountByTime);
        List<String> weeks = new ArrayList<>();
        List<Integer> accountByWeek = new ArrayList<>();
        for(volunteerDataCount temp : accountByTime){
            weeks.add(temp.getData());
            accountByWeek.add(temp.getAccounts());
        }
        SeriesData seriesData = new SeriesData("当日志愿者人数", accountByWeek, "bar", true, "blue");
        CharData charData = new CharData();
        charData.setXAxis(weeks);
        charData.setSeriesData(List.of(seriesData));
        Gson gson = new Gson();
        return gson.toJson(charData);
    }

    @Override
    public List<Volunteer> getVolunteerTodayRegisterList() {
        List<Volunteer> volunteerToDayRegisterList = mapper.getVolunteerToDayRegisterList();
        return volunteerToDayRegisterList;
    }
}

//        charData2.setMapName("志愿者评价等级统计");
//        int fiveStar  = mapper.findFiveStar(volunteerId);
//        int fourStar  = mapper.findFourStar(volunteerId);
//        int thereStar = mapper.findThereStar(volunteerId);
//        int towStar   = mapper.findTowStar(volunteerId);
//        int oneStar   = mapper.findOneStar(volunteerId);
//        PieChartData pieChartData  = new PieChartData("五星评价",fiveStar);
//        PieChartData pieChartData1 = new PieChartData("四星评价", fourStar);
//        PieChartData pieChartData2 = new PieChartData("三星评价", thereStar);
//        PieChartData pieChartData3 = new PieChartData("二星评价", towStar);
//        PieChartData pieChartData4 = new PieChartData("一星评价", oneStar);
//        List<PieChartData> list = Arrays.asList(pieChartData, pieChartData1, pieChartData2, pieChartData3, pieChartData4);
//        charData2.setPieChartData(list);